%% Copyright 2011 Steve Davis <steve@simulacity.com>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
% 
% http://www.apache.org/licenses/LICENSE-2.0
% 
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% qrcode_params.hrl

-record(qr_params, {mode, version, dimension, ec_level, block_defs, align_coords, remainder, mask, data}).

-define(QR_GF256_PRIME_MODULUS, 285). % 16#011D -> 2^8 + 2^4 + 2^3 + 2^2 + 1

-define(VERSION_INFO_POLY, 7973). % 16#1f25 -> 0001 1111 0010 0101
-define(FORMAT_INFO_POLY, 1335).  % 16#0537 -> 0000 0101 0011 1110
-define(FORMAT_INFO_MASK, 21522). % 16#5412 -> 0101 0100 0001 0010

-define(QUIET_ZONE, 4). % recommended value

%% Table 2. Mode Indicator
-define(TERMINATOR, 0).
-define(NUMERIC_MODE, 1).
-define(ALPHANUMERIC_MODE, 2).
-define(STRUCTURED_APPEND_MODE, 3).
-define(FNC1_FIRST_POSITION_MODE, 5).
-define(BYTE_MODE, 4).
-define(ECI_MODE, 7).
-define(KANJI_MODE, 8).
-define(FNC1_SECOND_POSITION_MODE, 9).

%% Table 3. Number of bits in Character Count Indicator
%% {Mode, [v0-v9, v10-v26, v27-v40]}
-define(CCI_BITSIZE, [
	{?NUMERIC_MODE, [10, 12, 14]},
	{?ALPHANUMERIC_MODE, [9, 11, 13]},
	{?BYTE_MODE, [8, 16, 16]},
	{?KANJI_MODE, [8, 16, 16]}
]).

% Table 5. Alphanumeric charset - see also char/1 
-define(CHARSET, <<"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:">>).
-define(ALPHANUMERIC_REGEX, <<"[", ?CHARSET/binary, "]+">>).
-define(NUMERIC_REGEX, <<"[0123456789]+">>).

% Section 8.4.9
-define(DATA_PAD_0, 236). % 11101100
-define(DATA_PAD_1, 17).  % 00010001

%% Annex E. Table E.1 - Origin at 1, 1 rather than 0, 0
-define(ALIGNMENT_COORDINATES, {
	[],
	[7, 19],
	[7, 23],
	[7, 27],
	[7, 31],
	[7, 35], 
	[7, 23, 39], % Version 7
	[7, 25, 43], 
	[7, 27, 47], 
	[7, 29, 51],
	[7, 31, 55], 
	[7, 33, 59], 
	[7, 35, 63], 
	[7, 27, 47, 67], % Version 14 
	[7, 27, 49, 71],
	[7, 27, 51, 75], 
	[7, 31, 55, 79], 
	[7, 31, 57, 83], 
	[7, 31, 59, 87], 
	[7, 35, 63, 91],
	[7, 29, 51, 73, 95], % Version 21
	[7, 27, 51, 75, 99], 
	[7, 31, 55, 79, 103], 
	[7, 29, 55, 81, 107], 
	[7, 33, 59, 85, 111],
	[7, 31, 59, 87, 115], 
	[7, 35, 63, 91, 119], 
	[7, 27, 51, 75, 99, 123], % Version 28
	[7, 31, 55, 79, 103, 127],
	[7, 27, 53, 79, 105, 131],
	[7, 31, 57, 83, 109, 135],
	[7, 35, 61, 87, 113, 139],
	[7, 31, 59, 87, 115, 143],
	[7, 35, 63, 91, 119, 147],
	[7, 31, 55, 79, 103, 127, 151], % Version 35
	[7, 25, 51, 77, 103, 129, 155],
	[7, 29, 55, 81, 107, 133, 159],
	[7, 33, 59, 85, 111, 137, 163],
	[7, 27, 55, 83, 111, 139, 167],
	[7, 31, 59, 87, 115, 143, 171] % Version 40
}).

% Composite of Tables 1, 7-11, 13-22
% {{level, version}, {numeric_capacity, alpha_capacity, byte_capacity, kanji_capacity}, ecc_blocks[{number_of_blocks, total_bytes, data_bytes], remainder_bits}
-define(TABLES, [
	{{'L',1},{41,25,17,10},[{1,26,19}],0},
	{{'L',2},{77,47,32,20},[{1,44,34}],7},
	{{'L',3},{127,77,53,32},[{1,70,55}],7},
	{{'L',4},{187,114,78,48},[{1,100,80}],7},
	{{'L',5},{255,154,106,65},[{1,134,108}],7},
	{{'L',6},{322,195,134,82},[{2,86,68}],7},
	{{'L',7},{370,224,154,95},[{2,98,78}],0},
	{{'L',8},{461,279,192,118},[{2,121,97}],0},
	{{'L',9},{552,335,230,141},[{2,146,116}],0},
	{{'L',10},{652,395,271,167},[{2,86,68},{2,87,69}],0},
	{{'L',11},{772,468,321,198},[{4,101,81}],0},
	{{'L',12},{883,535,367,226},[{2,116,92},{2,117,93}],0},
	{{'L',13},{1022,619,425,262},[{4,133,107}],0},
	{{'L',14},{1101,667,458,282},[{3,145,115},{1,146,116}],3},
	{{'L',15},{1250,758,520,320},[{5,109,87},{1,110,88}],3},
	{{'L',16},{1408,854,586,361},[{5,122,98},{1,123,99}],3},
	{{'L',17},{1548,938,644,397},[{1,135,107},{5,136,108}],3},
	{{'L',18},{1725,1046,718,442},[{5,150,120},{1,151,121}],3},
	{{'L',19},{1903,1153,792,488},[{3,141,113},{4,142,114}],3},
	{{'L',20},{2061,1249,858,528},[{3,135,107},{5,136,108}],3},
	{{'L',21},{2232,1352,929,572},[{4,144,116},{4,145,117}],4},
	{{'L',22},{2409,1460,1003,618},[{2,139,111},{7,140,112}],4},
	{{'L',23},{2620,1588,1091,672},[{4,151,121},{5,152,122}],4},
	{{'L',24},{2812,1704,1171,721},[{6,147,117},{4,148,118}],4},
	{{'L',25},{3057,1853,1273,784},[{8,132,106},{4,133,107}],4},
	{{'L',26},{3283,1990,1367,842},[{10,142,114},{2,143,115}],4},
	{{'L',27},{3517,2132,1465,902},[{8,152,122},{4,153,123}],4},
	{{'L',28},{3669,2223,1528,940},[{3,147,117},{10,148,118}],3},
	{{'L',29},{3909,2369,1628,1002},[{7,146,116},{7,147,117}],3},
	{{'L',30},{4158,2520,1732,1066},[{5,145,115},{10,146,116}],3},
	{{'L',31},{4417,2677,1840,1132},[{13,145,115},{3,146,116}],3},
	{{'L',32},{4686,2840,1952,1201},[{17,145,115}],3},
	{{'L',33},{4965,3009,2068,1273},[{17,145,115},{1,146,116}],3},
	{{'L',34},{5253,3183,2188,1347},[{13,145,115},{6,146,116}],3},
	{{'L',35},{5529,3351,2303,1417},[{12,151,121},{7,152,122}],0},
	{{'L',36},{5836,3537,2431,1496},[{6,151,121},{14,152,122}],0},
	{{'L',37},{6153,3729,2563,1577},[{17,152,122},{4,153,123}],0},
	{{'L',38},{6479,3927,2699,1661},[{4,152,122},{18,153,123}],0},
	{{'L',39},{6743,4087,2809,1729},[{20,147,117},{4,148,118}],0},
	{{'L',40},{7089,4296,2953,1817},[{19,148,118},{6,149,119}],0},
	{{'M',1},{34,20,14,8},[{1,26,16}],0},
	{{'M',2},{63,38,26,16},[{1,44,28}],7},
	{{'M',3},{101,61,42,26},[{1,70,44}],7},
	{{'M',4},{149,90,62,38},[{2,50,32}],7},
	{{'M',5},{202,122,84,52},[{2,67,43}],7},
	{{'M',6},{255,154,106,65},[{4,43,27}],7},
	{{'M',7},{293,178,122,75},[{4,49,31}],0},
	{{'M',8},{365,221,152,93},[{2,60,38},{2,61,39}],0},
	{{'M',9},{432,262,180,111},[{3,58,36},{2,59,37}],0},
	{{'M',10},{513,311,213,131},[{4,69,43},{1,70,44}],0},
	{{'M',11},{604,366,251,155},[{1,80,50},{4,81,51}],0},
	{{'M',12},{691,419,287,177},[{6,58,36},{2,59,37}],0},
	{{'M',13},{796,483,331,204},[{8,59,37},{1,60,38}],0},
	{{'M',14},{871,528,362,223},[{4,64,40},{5,65,41}],3},
	{{'M',15},{991,600,412,254},[{5,65,41},{5,66,42}],3},
	{{'M',16},{1082,656,450,277},[{7,73,45},{3,74,46}],3},
	{{'M',17},{1212,734,504,310},[{10,74,46},{1,75,47}],3},
	{{'M',18},{1346,816,560,345},[{9,69,43},{4,70,44}],3},
	{{'M',19},{1500,909,624,384},[{3,70,44},{11,71,45}],3},
	{{'M',20},{1600,970,666,410},[{3,67,41},{13,68,42}],3},
	{{'M',21},{1708,1035,711,438},[{17,68,42}],4},
	{{'M',22},{1872,1134,779,480},[{17,74,46}],4},
	{{'M',23},{2059,1248,857,528},[{4,75,47},{14,76,48}],4},
	{{'M',24},{2188,1326,911,561},[{6,73,45},{14,74,46}],4},
	{{'M',25},{2395,1451,997,614},[{8,75,47},{13,76,48}],4},
	{{'M',26},{2544,1542,1059,652},[{19,74,46},{4,75,47}],4},
	{{'M',27},{2701,1637,1125,692},[{22,73,45},{3,74,46}],4},
	{{'M',28},{2857,1732,1190,732},[{3,73,45},{23,74,46}],3},
	{{'M',29},{3035,1839,1264,778},[{21,73,45},{7,74,46}],3},
	{{'M',30},{3289,1994,1370,843},[{19,75,47},{10,76,48}],3},
	{{'M',31},{3486,2113,1452,894},[{2,74,46},{29,75,47}],3},
	{{'M',32},{3693,2238,1538,947},[{10,74,46},{23,75,47}],3},
	{{'M',33},{3909,2369,1628,1002},[{14,74,46},{21,75,47}],3},
	{{'M',34},{4134,2506,1722,1060},[{14,74,46},{23,75,47}],3},
	{{'M',35},{4343,2632,1809,1113},[{12,75,47},{26,76,48}],0},
	{{'M',36},{4588,2780,1911,1176},[{6,75,47},{34,76,48}],0},
	{{'M',37},{4775,2894,1989,1224},[{29,74,46},{14,75,47}],0},
	{{'M',38},{5039,3054,2099,1292},[{13,74,46},{32,75,47}],0},
	{{'M',39},{5313,3220,2213,1362},[{40,75,47},{7,76,48}],0},
	{{'M',40},{5596,3391,2331,1435},[{18,75,47},{31,76,48}],0},
	{{'Q',1},{27,16,11,7},[{1,26,13}],0},
	{{'Q',2},{48,29,20,12},[{1,44,22}],7},
	{{'Q',3},{77,47,32,20},[{2,35,17}],7},
	{{'Q',4},{111,67,46,28},[{2,50,24}],7},
	{{'Q',5},{144,87,60,37},[{2,33,15},{2,34,16}],7},
	{{'Q',6},{178,108,74,45},[{4,43,19}],7},
	{{'Q',7},{207,125,86,53},[{2,32,14},{4,33,15}],0},
	{{'Q',8},{259,157,108,66},[{4,40,18},{2,41,19}],0},
	{{'Q',9},{312,189,130,80},[{4,36,16},{4,37,17}],0},
	{{'Q',10},{364,221,151,93},[{6,43,19},{2,44,20}],0},
	{{'Q',11},{427,259,177,109},[{4,50,22},{4,51,23}],0},
	{{'Q',12},{489,296,203,125},[{4,46,20},{6,47,21}],0},
	{{'Q',13},{580,352,241,149},[{8,44,20},{4,45,21}],0},
	{{'Q',14},{621,376,258,159},[{11,36,16},{5,37,17}],3},
	{{'Q',15},{703,426,292,180},[{5,54,24},{7,55,25}],3},
	{{'Q',16},{775,470,322,198},[{15,43,19},{2,44,20}],3},
	{{'Q',17},{876,531,364,224},[{1,50,22},{15,51,23}],3},
	{{'Q',18},{948,574,394,243},[{17,50,22},{1,51,23}],3},
	{{'Q',19},{1063,644,442,272},[{17,47,21},{4,48,22}],3},
	{{'Q',20},{1159,702,482,297},[{15,54,24},{5,55,25}],3},
	{{'Q',21},{1224,742,509,314},[{17,50,22},{6,51,23}],4},
	{{'Q',22},{1358,823,565,348},[{7,54,24},{16,55,25}],4},
	{{'Q',23},{1468,890,611,376},[{11,54,24},{14,55,25}],4},
	{{'Q',24},{1588,963,661,407},[{11,54,24},{16,55,25}],4},
	{{'Q',25},{1718,1041,715,440},[{7,54,24},{22,55,25}],4},
	{{'Q',26},{1804,1094,751,462},[{28,50,22},{6,51,23}],4},
	{{'Q',27},{1933,1172,805,496},[{8,53,23},{26,54,24}],4},
	{{'Q',28},{2085,1263,868,534},[{4,54,24},{31,55,25}],3},
	{{'Q',29},{2181,1322,908,559},[{1,53,23},{37,54,24}],3},
	{{'Q',30},{2358,1429,982,604},[{15,54,24},{25,55,25}],3},
	{{'Q',31},{2473,1499,1030,634},[{42,54,24},{1,55,25}],3},
	{{'Q',32},{2670,1618,1112,684},[{10,54,24},{35,55,25}],3},
	{{'Q',33},{2805,1700,1168,719},[{29,54,24},{19,55,25}],3},
	{{'Q',34},{2949,1787,1228,756},[{44,54,24},{7,55,25}],3},
	{{'Q',35},{3081,1867,1283,790},[{39,54,24},{14,55,25}],0},
	{{'Q',36},{3244,1966,1351,832},[{46,54,24},{10,55,25}],0},
	{{'Q',37},{3417,2071,1423,876},[{49,54,24},{10,55,25}],0},
	{{'Q',38},{3599,2181,1499,923},[{48,54,24},{14,55,25}],0},
	{{'Q',39},{3791,2298,1579,972},[{43,54,24},{22,55,25}],0},
	{{'Q',40},{3993,2420,1663,1024},[{34,54,24},{34,55,25}],0},
	{{'H',1},{17,10,7,4},[{1,26,9}],0},
	{{'H',2},{34,20,14,8},[{1,44,16}],7},
	{{'H',3},{58,35,24,15},[{2,35,13}],7},
	{{'H',4},{82,50,34,21},[{4,25,9}],7},
	{{'H',5},{106,64,44,27},[{2,33,11},{2,34,12}],7},
	{{'H',6},{139,84,58,36},[{4,43,15}],7},
	{{'H',7},{154,93,64,39},[{4,39,13},{1,40,14}],0},
	{{'H',8},{202,122,84,52},[{4,40,14},{2,41,15}],0},
	{{'H',9},{235,143,98,60},[{4,36,12},{4,37,13}],0},
	{{'H',10},{288,174,119,74},[{6,43,15},{2,44,16}],0},
	{{'H',11},{331,200,137,85},[{3,36,12},{8,37,13}],0},
	{{'H',12},{374,227,155,96},[{7,42,14},{4,43,15}],0},
	{{'H',13},{427,259,177,109},[{12,33,11},{4,34,12}],0},
	{{'H',14},{468,283,194,120},[{11,36,12},{5,37,13}],3},
	{{'H',15},{530,321,220,136},[{11,36,12},{7,37,13}],3},
	{{'H',16},{602,365,250,154},[{3,45,15},{13,46,16}],3},
	{{'H',17},{674,408,280,173},[{2,42,14},{17,43,15}],3},
	{{'H',18},{746,452,310,191},[{2,42,14},{19,43,15}],3},
	{{'H',19},{813,493,338,208},[{9,39,13},{16,40,14}],3},
	{{'H',20},{919,557,382,235},[{15,43,15},{10,44,16}],3},
	{{'H',21},{969,587,403,248},[{19,46,16},{6,47,17}],4},
	{{'H',22},{1056,640,439,270},[{34,37,13}],4},
	{{'H',23},{1108,672,461,284},[{16,45,15},{14,46,16}],4},
	{{'H',24},{1228,744,511,315},[{30,46,16},{2,47,17}],4},
	{{'H',25},{1286,779,535,330},[{22,45,15},{13,46,16}],4},
	{{'H',26},{1425,864,593,365},[{33,46,16},{4,47,17}],4},
	{{'H',27},{1501,910,625,385},[{12,45,15},{28,46,16}],4},
	{{'H',28},{1581,958,658,405},[{11,45,15},{31,46,16}],3},
	{{'H',29},{1677,1016,698,430},[{19,45,15},{26,46,16}],3},
	{{'H',30},{1782,1080,742,457},[{23,45,15},{25,46,16}],3},
	{{'H',31},{1897,1150,790,486},[{23,45,15},{28,46,16}],3},
	{{'H',32},{2022,1226,842,518},[{19,45,15},{35,46,16}],3},
	{{'H',33},{2157,1307,898,553},[{11,45,15},{46,46,16}],3},
	{{'H',34},{2301,1394,958,590},[{59,46,16},{1,47,17}],3},
	{{'H',35},{2361,1431,983,605},[{22,45,15},{41,46,16}],0},
	{{'H',36},{2524,1530,1051,647},[{2,45,15},{64,46,16}],0},
	{{'H',37},{2625,1591,1093,673},[{24,45,15},{46,46,16}],0},
	{{'H',38},{2735,1658,1139,701},[{42,45,15},{32,46,16}],0},
	{{'H',39},{2927,1774,1219,750},[{10,45,15},{67,46,16}],0},
	{{'H',40},{3057,1852,1273,784},[{20,45,15},{61,46,16}],0}
]).

